<?php
/**
 * 消费订单提交接口
 */
namespace Api\Controller;

use Think\Controller;

class RFController extends Controller {
	
	public function _initialize() {
        $this->m_rf_card = D("Api/RfCard");
        $this->m_card = D("Admin/Card");
        $this->m_rf_admin = D("Api/RfAdmin");
	}

    public $return_code = array(
        '1000'=>'成功',
        '1001'=>'系统错误',
        '1002'=>'操作员不存在',
        '1003'=>'操作员密码错误',
        '1004'=>'操作员被锁定',
        '1005'=>'硬件号码已存在',
        '1006'=>'硬件号码不存在',
    );

    /*获取销售列表+分页*/
    public function write() {
        try{
        
            $username = I("post.username");
            $password = I("post.password");
            $hardid = I("post.hardid");
            $softid = I("post.softid");
            
            $admin_where = array(
                'username'=>$username,
            );

            $rf_admin = $this->m_rf_admin->where($admin_where)->find();

            if (empty($rf_admin)) {     // 操作员不存在
                exit(json_encode(array('code'=>1002,'msg'=>$this->return_code['1002'])));
            }elseif ($rf_admin['password'] != md5($password)) {     // 操作员密码错误
                exit(json_encode(array('code'=>1003,'msg'=>$this->return_code['1003'])));
            }elseif ($rf_admin['status'] != '1') {     // 操作员被锁定
                exit(json_encode(array('code'=>1004,'msg'=>$this->return_code['1004'])));
            }

            /*不限制硬件，保证重复读写*/
            /*不限制软件号码*/

            $card_where = array(
                'hardid'=>$hardid,
            );

            $rf_card = $this->m_rf_card->where($card_where)->find();

            if (!empty($rf_card)) {     // 硬件号码已存在
                // exit(json_encode(array('code'=>1005,'msg'=>$this->return_code['1005'])));
                $rf_update = array(
                    'hardid' => $hardid,
                    'softid' => $softid,
                    'admin_id' => $rf_admin['admin_id'],
                    'opt_time' => date('Y-m-d H:i:s',time()),
                );
                $flag = $this->m_rf_card->where($card_where)->save($rf_update);

            }else{
                $rf_insert = array(
                    'hardid' => $hardid,
                    'softid' => $softid,
                    'admin_id' => $rf_admin['admin_id'],
                    'opt_time' => date('Y-m-d H:i:s',time()),
                );

                $flag = $this->m_rf_card->add($rf_insert);
            }
            if ($flag) {
                exit(json_encode(array('code'=>1000,'msg'=>'成功，写入成功')));
            }else{
                exit(json_encode(array('code'=>1001,'msg'=>'失败，写入失败')));
            }

        }catch (\Exception $e){
            exit(json_encode(array('code'=>1001,'msg'=>$this->return_code['1001'])));
        }
    }

    /*销售前计算*/
    public function read() {
        try{
        
            $hardid = I("post.hardid");
            
            $card_where = array(
                'hardid'=>$hardid,
            );

            $rf_card = $this->m_rf_card->where($card_where)->find();

            if (empty($rf_card)) {     // 硬件号码不存在
                /*查询本地卡*/
                // $normal_card_where = array(
                //     'number' => $hardid,
                // );
                // $normal_card = $this->m_card->where($normal_card_where)->find();
                // if ($normal_card) {

                // }else{

                // }
                if (strlen($hardid) == 10) {
                    exit(json_encode(array('code'=>1000,'msg'=>'成功，其他卡读取成功','softid'=>$hardid)));
                }else{
                    exit(json_encode(array('code'=>1006,'msg'=>$this->return_code['1006'],'softid'=>'')));
                }
            }

            exit(json_encode(array('code'=>1000,'msg'=>'成功，读取成功','softid'=>$rf_card['softid'])));

        }catch (\Exception $e){
            exit(json_encode(array('code'=>1001,'msg'=>$this->return_code['1001'],'softid'=>'')));
        }
    }

}

